package cn.stronglink.oil.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import cn.stronglink.oil.R;


/**
 * Created by Tgb on 2016/11/13.
 */

public class ExceptionHintDialog extends Dialog implements View.OnClickListener {
    private Button btn_ok;
    private TextView tv_title;
    private TextView tv_message;
    private OnOkClickListener onOkClickListener;
    private String customMessage,title;                      // 外部自定义的信息

    public ExceptionHintDialog(Context context) {
        this(context,0);
    }

    public ExceptionHintDialog(Context context, int closeTime){
        super(context, R.style.MyDialog);
        this.runDownTimeCloseDialog(closeTime);
    }

    private void runDownTimeCloseDialog(final int closeTime){
        if(closeTime<1) return;

        new Thread(new Runnable() {
            int time;
            @Override
            public void run() {
                while(true){
                    if(time++>=closeTime){
                        ExceptionHintDialog.this.dismiss();
                        break;
                    }

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    public void setOnOkClickListener(OnOkClickListener onOkClickListener) {
        this.onOkClickListener = onOkClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_ok);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(true);
        initView();
        initData();
    }

    private void initView() {
        tv_title = (TextView) findViewById(R.id.tv_dialog_title);
        btn_ok = (Button) findViewById(R.id.btn_ok);
        tv_message = (TextView) findViewById(R.id.tv_message);
        btn_ok.setOnClickListener(this);
    }

    /**
     * 设置数据
     */
    private void initData() {
        if (title != null) {
            tv_title.setText(title);
        }
        if (customMessage != null) {
            tv_message.setText(customMessage);
        }
    }

    /**
     * 设置信息
     *
     * @param msg
     */
    public void setDialogMessage(String msg) {
        this.customMessage = msg;
    }

    public void setDialogTitle(String title) {
        this.title = title;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ok:
                if (this.onOkClickListener != null) {
                    this.onOkClickListener.onOkClick();
                }
                break;
        }
    }

    public interface OnOkClickListener {
        void onOkClick();
    }
}
